package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.BrandDTO;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.entity.Category;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     * 根据品牌id查询品牌
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> querByBrandId(@PathVariable("id")Long id){
        Brand brand = brandService.getById(id);
        return ResponseEntity.ok().body(new BrandDTO(brand));
    }

    /**根据品牌id集合查询品牌集合
     *
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryByBrandIds(@RequestParam("ids") List<Long> ids){
        List<Brand> listBrand = brandService.listByIds(ids);
        return ResponseEntity.ok()
                .body(listBrand.stream()
                        .map(BrandDTO::new)
                        .collect(Collectors.toList()));
    }

    /**
     *  分页查询
     */
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDTO>> queryByPage(
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "key", required = false)String key
    ){
        return ResponseEntity.ok().body(brandService.queryByPage(page, rows, key));

    }

    /**
     * 新增品牌
     */
    @PostMapping
    public ResponseEntity<Void> addNewBrand(BrandDTO brandDTO){
        try {
            brandService.addOrUpdate(brandDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新品牌
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        try {
            brandService.addOrUpdate(brandDTO);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.status(HttpStatus.OK).build();

    }

    /**
     * 根据id删除品牌
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
        try {
            brandService.removeById(id);
        } catch (Exception e) {
            return ResponseEntity.status(401).build();
        }
        return ResponseEntity.status(204).build();
    }

    /**
     * 获取db中的图片信息
     */

    @GetMapping("brandList")
    public List<String> brandList(){
        return brandService.getBrands();
    }

    /**
     *根据id查询分类信息
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandById(@RequestParam("id") Integer cid){

        List<Long> category_id = categoryBrandService
                .list(new QueryWrapper<CategoryBrand>()
                        .eq("category_id", cid))
                .stream()
                .map(categoryBrand -> categoryBrand.getBrandId())
                .collect(Collectors.toList());
        List<BrandDTO> brandDTOS = brandService.listByIds(category_id).stream().map(BrandDTO::new).collect(Collectors.toList());
        return ResponseEntity.ok().body(brandDTOS);
    }

}
